package com.jiezuo.common.util;

import com.alibaba.fastjson.JSON;
import com.jiezuo.auth.entity.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
* @description   通过RequestContextHolder:持有上下文的Request容器,获取request
* @author        庞留杰
* @da用户信息te          2023/7/24 8:43
* @version       V1.0
**/
public class ContextHolderUtils {

    public static HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    // 获取当前用户信息
    public static User currentUser() {
        User user = new User();
        if (RequestContextHolder.getRequestAttributes() != null) { // 防止使用@PostConstruct注解报错，在项目启动时调用RequestContextHolder.getRequestAttributes()为空
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            Object obj = request.getAttribute("user");
            user = JSON.parseObject(obj + "", User.class);
        }
        return user;
    }

    // 获取当前用户组信息
    public static String getGroupIds() {
        String group_ids = "";
        if (RequestContextHolder.getRequestAttributes() != null) { // 防止使用@PostConstruct注解报错，在项目启动时调用RequestContextHolder.getRequestAttributes()为空
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            group_ids = request.getAttribute("group_ids")+"";
        }
        return group_ids;
    }

    // 获取当前用户组信息
    public static String getFlowGroupIds() {
        String flow_group_ids = "";
        if (RequestContextHolder.getRequestAttributes() != null) { // 防止使用@PostConstruct注解报错，在项目启动时调用RequestContextHolder.getRequestAttributes()为空
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            flow_group_ids = request.getAttribute("flow_group_ids")+"";
        }
        return flow_group_ids;
    }

}
